<?php
/**
 * swoole通用服务
 * @author chendanming@star-net.cn
 * @copyright  ktvme. All Rights Reserved.
 * @version $Id: Server.php 629 2018-05-03 08:14:13Z chendanming $
 */

namespace app\console\swooletask;

use app\console\common\ServerCommand;
use think\Log;

class Server extends ServerCommand {
    private $swooleTaskPath = '\\app\console\\swooletask\\task\\';

    protected function configure() {
        $this->setName('kmads-common-swoole')->setDescription('swoole通用服务');
    }

    public function onReceive($serv, $fd, $from_id, $data) {
        //        Log::record(['more_info' => ['swoole_server_onreceive'=>$data]], 'elk');
        //        Log::save();
        $data  = trim($data);
        $datas = json_decode($data, true);

        switch($datas['type']) {
            case 'Email': {
                $taskClass = $this->swooleTaskPath . 'email\\' . $datas['type'] . 'Task';
                call_user_func_array(array(
                    new $taskClass(),
                    'index'
                ), array(
                    $serv,
                    $datas['data']
                ));
                break;
            }
            case 'WechatTag': {
                $taskClass = $this->swooleTaskPath . 'wechat\\' . $datas['type'] . 'Task';
                call_user_func_array(array(
                    new $taskClass(),
                    'index'
                ), array(
                    $serv,
                    $datas['data']
                ));
                break;
            }
            case 'WechatUserInfo': {
                $taskClass = $this->swooleTaskPath . 'wechat\\' . $datas['type'] . 'Task';
                call_user_func_array(array(
                    new $taskClass(),
                    'index'
                ), array(
                    $serv,
                    $datas['data']
                ));
                break;
            }
            case 'WechatUserInfoAgain': {
                $taskClass = $this->swooleTaskPath . 'wechat\\' . $datas['type'] . 'Task';
                call_user_func_array(array(
                    new $taskClass(),
                    'index'
                ), array(
                    $serv,
                    $datas['data']
                ));
                break;
            }
            case 'InitSchedule' : {
                $taskClass = $this->swooleTaskPath . 'inventory\\' . $datas['type'] . 'Task';
                call_user_func_array(array(
                    new $taskClass(),
                    'index'
                ), array(
                    $serv,
                    $datas['data']
                ));
                break;
            }
            case 'InitCompany' : {
                $taskClass = $this->swooleTaskPath . 'inventory\\' . $datas['type'] . 'Task';
                call_user_func_array(array(
                    new $taskClass(),
                    'index'
                ), array(
                    $serv,
                    $datas['data']
                ));
                break;
            }
            default:
                return [
                    'type'   => 'undefided',
                    'status' => false
                ];
        }
    }

    public function onTask($serv, $task_id, $src_worker_id, $data) {
        $className = $data['class'];
        $action    = $data['action'];

        $model = $className . '\\' . $action;
        if($this->_isAddLog($action)) {
            $this->addLogStartForConsole($model);
        }

        $task = call_user_func_array(array(
            new $className(),
            $action
        ), array($data['datas']));

        if($this->_isAddLog($action)) {
            $this->addLogEndForConsole();
        }

        return $task;
    }

    /**
     * 是否需要记录log
     * @param $action
     */
    private function _isAddLog($action) {
        $noLogActionArr = ['wechatTag'];

        if(in_array($action, $noLogActionArr)) {
            return false;
        } else {
            return true;
        }
    }
}